package com.book.bookprojectapplication.write.adapter;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.book.bookprojectapplication.R;
 
public class BestAdapter extends ArrayAdapter {
 
    // Declare Variables
    Context context;
    LayoutInflater inflater;
    ArrayList<HashMap<String, String>> data;
    TextView bookName;
    TextView bookPub;
    TextView bookWriter;
    ImageView imagev;

 
    public BestAdapter(Context context, int textViewResourceId,
            ArrayList<HashMap<String, String>> arraylist) {
    	super(context, textViewResourceId, arraylist);
        this.context = context;
        data = arraylist;
 
    }
 
    
    public int getCount() {
        return data.size();
    }
 
   
    public Object getItem(int position) {
        return null;
    }
 
   
    public long getItemId(int position) {
        return 0;
    }
 
    public View getView(final int position, View convertView, ViewGroup parent) {
        // Declare Variables
     
 
        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
 
        View itemView = inflater.inflate(R.layout.form, parent, false);
        // Get the position from the results
        HashMap<String, String> resultp = new HashMap<String, String>();
        resultp = data.get(position);
 
        // Locate the TextViews in listview_item.xml
        bookName = (TextView) itemView.findViewById(R.id.fbook_name); 
        bookPub = (TextView) itemView.findViewById(R.id.fbook_pub);
        bookWriter = (TextView) itemView.findViewById(R.id.fbook_writer);
        imagev = (ImageView) itemView.findViewById(R.id.fimage_view);
        // Locate the ImageView in listview_item.xml
 
        // Capture position and set results to the TextViews
        bookName.setText(resultp.get("bookName"));
        bookPub.setText(resultp.get("bookPub"));
        bookWriter.setText(resultp.get("bookWriter"));
        imagev.setImageBitmap(getImageFromURL(resultp.get("imagev")));
        imagev.setScaleType(ImageView.ScaleType.FIT_CENTER);
        
        // Capture position and set results to the ImageView
        // Passes flag images URL into ImageLoader.class to download and cache
        // images
        // Capture button clicks on ListView items
        
 
        return itemView;
    }
    
    public Bitmap getImageFromURL(String imageURL){
        Bitmap imgBitmap = null;
        HttpURLConnection conn = null;
        BufferedInputStream bis = null;
        
        try
        {
            URL url = new URL(imageURL);
            conn = (HttpURLConnection)url.openConnection();
            conn.connect();
            
            int nSize = conn.getContentLength();
            bis = new BufferedInputStream(conn.getInputStream(), nSize);
            imgBitmap = BitmapFactory.decodeStream(bis);
        }
        catch (Exception e){
        	imagev.setImageResource(R.drawable.ic_launcher);
        } finally{
            if(bis != null) {
                try {bis.close();} catch (IOException e) {}
            }
            if(conn != null ) {
                conn.disconnect();
            }
        }
        
        return imgBitmap;
    }
}